<?php

namespace App\Http\Controllers\Backend\Flaw;

use App\Models\Ogz;
use App\Services\ChartAnalysis\Algorithm;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Flaw\Vuls;
use App\Http\Controllers\Traits\Base;
use App\Services\ChartAnalysis\ChartAnalysis;
use Illuminate\Support\Facades\DB;

class ChartsAnalysisController extends Controller
{
    use Base;

    const SUBLEVEL = 2;
    private $chartService;
    function __construct () {
        $this->chartService = new ChartAnalysis();
    }
    /**
     * index  集团图表展示
     */
    public function index(Request $request,$dashboard = false){
        $id = $request->id ? $request->id : 1;
        $info = DB::table('ogz')->where(['id'=>$id])->first();
        if ($request->startTime && $request->endTime) {
            //如果request对象包含时间参数，设置setTimeVaild
            $this->chartService->setTimeVaild();
            $this->chartService->startime = $request->startTime;
            $this->chartService->endTime = $request->endTime;
        } else {
            //如果request对象没有时间参数或者时间参数为空，设置setTimeVaildOut
            $this->chartService->setTimeVaildOut();
        }
        //one
        $one = $this->chartService->getPieByStatus($id);
        $pieStatus['name'] = collect($one)->pluck('oname');
        $pieStatus['confirm'] = collect($one)->pluck('confirm_index');
        $pieStatus['count'] = collect($one)->pluck('count_index');
        //two
        $graphNumTemp = $this->chartService->getGraphByNum($id);

        $graphNum['id'] = collect($graphNumTemp)->pluck('oid');
        $graphNum['name'] = collect($graphNumTemp)->pluck('oname');
        $graphNum['high'] = collect($graphNumTemp)->pluck('high');
        $graphNum['secondary'] = collect($graphNumTemp)->pluck('secondary');
        $graphNum['low'] = collect($graphNumTemp)->pluck('low');
        //three
        $oneTemp = $this->chartService->getPieByType($id);
        $pieType['name'] = collect($oneTemp)->pluck('name');
        $pieType['value'] = collect($oneTemp)->pluck('value_index');
        //four
        $diagram = $this->chartService->getDiagram($id);
        //staticSyn
        $algorithm = new Algorithm();

        $staticSyn = $algorithm->staticSynthesized([$request->startTime,$request->endTime]);
        $data = [
            'pieStatus'=>json_encode($pieStatus),
            'graphNum'=>json_encode($graphNum),
            'pieType'=>json_encode($pieType),
            'diagram'=>json_encode($diagram),
            'info'=>$info,
            'topNum'=>count($oneTemp),
            'staticSyn' => $staticSyn
        ];

        if ($dashboard){
            return $data;
        }else{

            return view('backend.charts.index',$data);
        }
    }

    /**
     * subBlockStatistics  子版块图表展示
     */
    public function  subBlockStatistics(Request $request,$dashboard = false){
        //和当前子级元素取交集 避免取到其他同级别下元素
        $pid = isset($request->pid) ? $request->pid : 1;
        $hasOgz = access()->hasOgzs();
        $deep = Ogz::getDeepByPid(intval($pid));

        //权限级别和深度级别取交集
        $userOgz = array_intersect($hasOgz,Ogz::getByDeep($deep));
        //实现展示同级下拉筛选功能
        $childOgzLevel = Ogz::getDeepsByIds($userOgz);
        //当前板块子级
        $curSub = Ogz::where(['pid'=>$pid])->get();
        $eqOgz = [];
        foreach ($curSub as $item){
            if (in_array($item->id,$childOgzLevel)){
                $eqOgz[] = $item;
            }
        }
        //目前只兼容并级板块的情况,默认第二层是板块
        if (empty($eqOgz)){
            return view('backend.welcome');
        }
        $id = $request->id ? $request->id : $eqOgz[0]->id;

        $info = DB::table('ogz')->where(['id'=>$id])->first();
        if ($request->startTime && $request->endTime) {
            //如果request对象包含时间参数，设置setTimeVaild

            $this->chartService->setTimeVaild();
            $this->chartService->startime = $request->startTime;
            $this->chartService->endTime = $request->endTime;
        } else {

            //如果request对象没有时间参数或者时间参数为空，设置setTimeVaildOut
            $this->chartService->setTimeVaildOut();
        }
        //one漏洞和类型
        $oneTemp = $this->chartService->getPieByType($id);

        $pieType['name'] = collect($oneTemp)->pluck('name');
        $pieType['value'] = collect($oneTemp)->pluck('value_index');

        //two
        $diagram = $this->chartService->getDiagram($id);

        //three
        //柱状图
        $temp1 = $this->chartService->getGraphByNum($id);

        //折线图
        $temp2 = $this->chartService->getTacByTime($id);

        $mixd['id'] = collect($temp1)->pluck('oid');
        $mixd['name'] = collect($temp1)->pluck('oname');
        $mixd['high'] = collect($temp1)->pluck('high');
        $mixd['secondary'] = collect($temp1)->pluck('secondary');
        $mixd['low'] = collect($temp1)->pluck('low');
        $mixd['jumpSub'] = collect($temp1)->pluck('jumpSub');
        $mixd['total'] = collect($temp2)->pluck('total');
        $mixd['confirmed'] = collect($temp2)->pluck('confirmed');
        $data = [
            'pieType'=>json_encode($pieType),
            'diagram'=>json_encode($diagram),
            'mixed'=>json_encode($mixd),
            'info'=>$info,
            'eqOgz' => $eqOgz,
            'pid' => $id,
            'topNum'=>count($oneTemp)
        ];
        if ($request->dashboard){
            return $data;
        }else{

            return view('backend.charts.subblockcharts',$data);
        }

    }

    /**
     * companyStatistics 公司图表展示
     */
    public function companyStatistics(Request $request,$dashboard = false){
        if (!isset($request->id)) {
            $ogz = new Ogz();
            $hasOgz = access()->hasOgzs();
            //权限级别和深度级别取交集,默认公司是最后一层,no children
            $userOgz = array_intersect($hasOgz,Ogz::getByDeep(Ogz::getBigestDeep()));

            $company = [];
            foreach ($userOgz as $k => $v){
                if(!$ogz->hasChild($v)){
                    $company[] = $v;
                }
            }
            if (empty($company)){
                return view('backend.welcome');
            }
        }
        $id = $request->id ? $request->id : $company[0];
        $info = DB::table('ogz')->where(['id'=>$id])->first();
        if ($request->startTime && $request->endTime) {
            //如果request对象包含时间参数，设置setTimeVaild

            $this->chartService->setTimeVaild();
            $this->chartService->startime = $request->startTime;
            $this->chartService->endTime = $request->endTime;
        } else {

            //如果request对象没有时间参数或者时间参数为空，设置setTimeVaildOut
            $this->chartService->setTimeVaildOut();
        }
        //柱状图
        $graph = $this->chartService->getGraphByLevel($id);
        //饼图
        $pietype = $this->chartService->getPieByType($id);

        $pie['name'] = collect($pietype)->pluck('name');
        $pie['value'] = collect($pietype)->pluck('value_index');
        //曲线走势图
        $diagram = $this->chartService->getDiagram($id);
        $appId = DB::table('app')->where(['ogz_id'=>$id])->pluck('id');
        $apps = $this->userLinks();
        $vlists = Vuls::unionSelect($appId->intersect($apps['apps'])->toArray());
        $vlists['export'] = $vlists['export']->whereIn('status',[1,2,3,4,5,6,7,8,9,10,11])->all();
        $data = [
            'graphNum' => json_encode($graph['other']),
            'pieType' => json_encode($pie),
            'diagram' => json_encode($diagram),
            'lists'=>$vlists['export'],
            'vulStatus' => Vuls::$vulsStatus,
            'info'=>$info,
            'topNum'=>count($pietype)
        ];
        if ($request->dashboard){
            return $data;
        }else{

            return view('backend.charts.companycharts',$data);
        }
    }
}
